/*
 * Copyright (c) 2015. For Intelligent Group.
 */

package com.intelligent.ispc.web.controller;

import com.google.common.collect.Lists;
import com.intelligent.ispc.common.dto.JsonPageResponseDto;
import com.intelligent.ispc.common.dto.JsonResponseDto;
import com.intelligent.ispc.common.utils.ApplicationException;
import com.intelligent.ispc.common.utils.ExceptionMessages;
import com.intelligent.ispc.common.web.Servlets;
import com.intelligent.ispc.core.dto.ExportFileDto;
import com.intelligent.ispc.core.service.ExportFileService;
import com.intelligent.ispc.utils.SystemConfig;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by Peter.Li on 2015/10/21.
 */
@Controller
@RequestMapping(value = "/exportfile")
public class ExportFileController {

    @Autowired
    private ExportFileService exportFileService;

    @Autowired
    private SystemConfig systemConfig;

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public String exportResult(ServletRequest request, Model model) {
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isPermitted("analysis:spc:export")) {
            return "error/noPermission";
        }

        return "analysis/export/exportResult";
    }

    @ResponseBody
    @RequestMapping(value = "/list")
    public JsonPageResponseDto getExportResultList(ServletRequest request, Model model) {
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isPermitted("analysis:spc:export")) {
            return JsonPageResponseDto.FAILURE().setMessage(ExceptionMessages.EXCEPTION_NO_PERMISSION);
        }

        List<ExportFileDto> dtoList = Lists.newArrayList();
        try {
            Map<String, Object> searchParam = Servlets.getParametersStartingWith(request, "search_");

            Integer start = 0;
            Integer draw = 0;
            if (StringUtils.isNotBlank(request.getParameter("start"))) {
                start = Integer.valueOf(request.getParameter("start"));
            }
            Integer size = systemConfig.getPageSize();
            if (StringUtils.isNotBlank(request.getParameter("length"))) {
                size = Integer.valueOf(request.getParameter("length"));
            }

            if (StringUtils.isNotBlank(request.getParameter("draw"))) {
                draw = Integer.valueOf(request.getParameter("draw"));
            }

            Integer pageNum = 0;
            if ((start / size) > 0) {
                pageNum = start / size;
            }
            Sort sort = new Sort(Sort.Direction.DESC, "createTime");
            Pageable pageable = new PageRequest(pageNum, size, sort);

            Page<ExportFileDto> page = exportFileService.searchFile(searchParam, pageable);
            dtoList = page.getContent();

            JsonPageResponseDto json = JsonPageResponseDto.SUCCESS().setBody(dtoList);
            json.setRecordsTotal(page.getTotalElements());
            json.setRecordsFiltered(page.getTotalElements());
            json.setDraw(0);
            return json;
        } catch (ApplicationException e) {
            return JsonPageResponseDto.FAILURE().setMessage(e.getMessage());
        }
    }

    @ResponseBody
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    public JsonResponseDto delExportResultList(@PathVariable("id") String id, ServletRequest request, Model model) {
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isPermitted("analysis:spc:export")) {
            return JsonResponseDto.FAILURE().setMessage(ExceptionMessages.EXCEPTION_NO_PERMISSION);
        }

        try {
            exportFileService.deleteExportFile(new Long(id));
        } catch (ApplicationException e) {
            return JsonResponseDto.FAILURE().setMessage(e.getMessage());
        }
        JsonResponseDto json = JsonResponseDto.SUCCESS();

        return json;
    }
}
